﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(LocalTransform))]
public class LocalTransformEditor : Editor
{
    public override void OnInspectorGUI()
    {
        var mm = (LocalTransform)target;
        base.OnInspectorGUI();
        if (GUILayout.Button("SetMin"))
        {
            mm.SetMin();
        }
        if (GUILayout.Button("SetMax"))
        {
            mm.SetMax();
        }
    }
}
#endif

public class LocalTransform : MonoBehaviour
{
    [Range(-1, 1)]
    public float value;

    public Tran2 min;
    public Tran2 max;
    public Tran2 origin;
    Matrix4x4 matrix;

    public Tran2 relative;
    public bool setValue;    

    private void Start()
    {
        origin = transform.LocalTran2();
        matrix = origin;
    }
    public void SetMin()
    {
        min = relative.copy;
    }
    public void SetMax()
    {
        max = relative.copy;
    }
    void Update()
    {
        //transform.SetTran2Local(relative);

        if (setValue)
        {
            Tran2 t = matrix * relative;
            transform.SetTran2Local(t);
        }
        else
        {
            //relative = matrix / transform.Tran2Local();
            if (value > 0)
                transform.SetTran2Local(matrix * (max * value));
            else
                transform.SetTran2Local(matrix * (min * -value));
        }
    }
}
